/*
 * @Author: yanfeixin
 * @Date: 2025-04-15 20:35:49
 * @LastEditTime: 2025-04-21 19:53:13
 * @LastEditors: yanfeixin
 * @Description:
 */
import type { Locale } from 'ant-design-vue/es/locale'
import type { App } from 'vue'
import { localesMap } from '@/locales'
import { setupI18n } from '@oaexpert/locales'
import antdDefaultLocale from 'ant-design-vue/es/locale/zh_CN'
import antdHKLocale from 'ant-design-vue/es/locale/zh_HK'
import dayjs from 'dayjs'
import { ref } from 'vue'

export const antdLocale = ref<Locale>(antdDefaultLocale)

async function loadAntdLocale(lang: LocaleType) {
  switch (lang) {
    case 'zh-CN':
      antdLocale.value = antdDefaultLocale
      break
    case 'zh-HK':
      antdLocale.value = antdHKLocale
      break
  }
}
async function loadDayjsLocale(lang: LocaleType) {
  let locale
  switch (lang) {
    case 'zh-CN':
      locale = await import('dayjs/locale/zh-cn')
      break
    case 'zh-HK':
      locale = await import('dayjs/locale/zh-hk')
      break
    case 'en-US':
      locale = await import('dayjs/locale/en')
    //   break
  }
  if (locale) {
    dayjs.locale(locale.default)
  }
  else {
    console.error(`Failed to load dayjs locale for ${lang}`)
  }
}
async function loadMessages(lang: LocaleType) {
  await loadAntdLocale(lang)
  await loadDayjsLocale(lang)
  const message = await localesMap[lang]?.()
  return localesMap[lang] ? message.default : {}
}
export async function setupLocale(app: App) {
  await setupI18n(app, {
    defaultLocale: 'zh-CN',
    loadMessages,
  })
}
